﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EasyClass {
	public partial class MainForm : Form {
		Controller controller;

		public MainForm(Controller c) {
			//system initialization
			InitializeComponent();

			controller = c;
			controller.SetMainForm(this);
		}

		public void AddTask(string taskName) {
			listBox1.Items.Add(taskName);
			listBox1.SetSelected(listBox1.Items.Count - 1, true);
			listBox1.Refresh();
		}

		public void ShowTask(int index) {
			if (index != ListBox.NoMatches) {  //valid click
				string taskName = listBox1.Text;
				TabPage tabPage = GetTabPageByTaskName(taskName);
				if (tabPage == null) {
					tabPage = new TabPage("Task: " + taskName);
					tabPage.AutoScroll = true;
					tabPage.BackColor = Color.White;
					tabControl1.Controls.Add(tabPage);
					UpdateTabPage(controller.GetTask(taskName));
				}
				tabControl1.SelectedTab = tabPage;
			}
		}

		public string GetSelectedTaskName() {
			if (listBox1.SelectedItem == null) return "";
			return listBox1.SelectedItem.ToString();
		}

		public void AddTopic(string topicName) {
			listBox2.Items.Add(topicName);
			listBox2.SetSelected(listBox2.Items.Count - 1, true);
			listBox2.Refresh();
		}

		public string GetSelectedTopicName() {
			if (listBox2.SelectedItem == null) return "";
			return listBox2.SelectedItem.ToString();
		}

		public void ShowTopic(int index) {
			if (index != ListBox.NoMatches) {  //valid click
				string topicName = listBox2.Text;
				TabPage tabPage = GetTabPageByTopicName(topicName);
				if (tabPage == null) {
					tabPage = new TabPage("Topic: " + topicName);
					tabPage.AutoScroll = true;
					tabPage.BackColor = Color.White;
					tabControl1.Controls.Add(tabPage);
					UpdateTabPage(controller.GetTopic(topicName));
				}
				tabControl1.SelectedTab = tabPage;
			}
		}

		private void Form1_Load(object sender, EventArgs e) {

		}

		/*private void button5_Click(object sender, EventArgs e) {
			Close();
		}

		private void button1_Click(object sender, EventArgs e) {
			NewTaskForm newTaskForm = new NewTaskForm(controller);
			newTaskForm.ShowDialog();
		}

		private void button2_Click(object sender, EventArgs e) {
			if (listBox1.Items.Count == 0) {
				MessageBox.Show("No task exists!");
				return;
			}
			AddProblemsForm addProblemForm = new AddProblemsForm(controller, listBox1.SelectedIndex);
			addProblemForm.ShowDialog();
		}
		 * 
		 * */

		private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e) {
			ShowTask(listBox1.IndexFromPoint(e.Location));
			
		}

		private TabPage GetTabPageByTaskName(string taskName) {
			TabControl.TabPageCollection tabpages = tabControl1.TabPages;
			for (int i = 0; i < tabpages.Count; ++i) {
				if (tabpages[i].Text == "Task: " + taskName) return tabpages[i];
			}
			return null;
		}

		private TabPage GetTabPageByTopicName(string topicName) {
			TabControl.TabPageCollection tabpages = tabControl1.TabPages;
			for (int i = 0; i < tabpages.Count; ++i) {
				if (tabpages[i].Text == "Topic: " + topicName) return tabpages[i];
			}
			return null;
		}

		private TabPage GetTabPageAnalysisByTaskName(string taskName) {
			TabControl.TabPageCollection tabpages = tabControl1.TabPages;
			for (int i = 0; i < tabpages.Count; ++i) {
				if (tabpages[i].Text == "Analysis: " + taskName) return tabpages[i];
			}
			return null;
		}

		public void UpdateTabPage(Task task) {
			TabPage tabPage = GetTabPageByTaskName(task.taskName);
			if (tabPage == null) return;  //not on tabControl currently
			tabPage.Controls.Clear();
			tabPage.Controls.Add(task.GetPanel());
		}

		public void UpdateTabPage(Topic topic) {
			TabPage tabPage = GetTabPageByTopicName(topic.topicName);
			if (tabPage == null) return;  //not on tabControl currently
			tabPage.Controls.Clear();
			tabPage.Controls.Add(topic.GetPanel());
		}

		public void UpdateTabPage(AllAnswer allAnswer) {
			Task task = allAnswer.task;
			TabPage tabPage = GetTabPageAnalysisByTaskName(task.taskName);
			if (tabPage == null) return;  //not on tabControl currently
			tabPage.Controls.Clear();
			tabPage.Controls.Add(allAnswer.GetPanel());
		}

		/*private void button3_Click(object sender, EventArgs e) {
			NewTopicForm newTopicForm = new NewTopicForm(controller);
			newTopicForm.ShowDialog();
		}

		private void button6_Click(object sender, EventArgs e) {
			if (listBox1.Items.Count == 0) {
				MessageBox.Show("No task exists!");
				return;
			}
			ExportTaskForm exportTaskForm = new ExportTaskForm(controller);
			exportTaskForm.ShowDialog();
		}

		private void welcomePage_Click(object sender, EventArgs e) {

		}
		
		private void listBox1_SelectedIndexChanged(object sender, EventArgs e) {

		}

		private void button7_Click(object sender, EventArgs e) {
			OpenFileDialog ofd = new OpenFileDialog();
			ofd.Filter = "Task File(*.task)|*.task";
			if (ofd.ShowDialog() == DialogResult.OK) {
				string localFilePath = ofd.FileName.ToString(); //acquire file path
				controller.TryImportTask(localFilePath);
			}
		}
		*/
		private void newTaskToolStripMenuItem_Click(object sender, EventArgs e) {
			NewTaskForm newTaskForm = new NewTaskForm(controller);
			newTaskForm.ShowDialog();
		}

		private void newProblemToolStripMenuItem_Click(object sender, EventArgs e) {
			if (listBox1.Items.Count == 0) {
				MessageBox.Show("No task exists!");
				return;
			}
			AddProblemsForm addProblemForm = new AddProblemsForm(controller, listBox1.SelectedIndex);
			addProblemForm.ShowDialog();
		}

		private void newTopicToolStripMenuItem_Click(object sender, EventArgs e) {
			NewTopicForm newTopicForm = new NewTopicForm(controller);
			newTopicForm.ShowDialog();
		}

		private void importTaskToolStripMenuItem_Click(object sender, EventArgs e) {
			OpenFileDialog ofd = new OpenFileDialog();
			ofd.Filter = "Task File(*.tsk)|*.tsk";
			if (ofd.ShowDialog() == DialogResult.OK) {
				string localFilePath = ofd.FileName.ToString(); //acquire file path
				if (controller.TryImportTask(localFilePath)) ShowTask(listBox1.Items.Count);
			}
		}

		private void exportTaskToolStripMenuItem_Click(object sender, EventArgs e) {
			if (listBox1.Items.Count == 0) {
				MessageBox.Show("No task exists!");
				return;
			}
			ExportTaskForm exportTaskForm = new ExportTaskForm(controller);
			exportTaskForm.ShowDialog();
		}

		private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
			Close();
		}

		private void label2_Click(object sender, EventArgs e) {

		}

		private void listBox1_DrawItem(object sender, DrawItemEventArgs e) {
			e.DrawBackground();
			if (e.Index < 0) return;
			string taskName = listBox1.Items[e.Index].ToString();
			Task task = controller.GetTask(taskName);
			int status = 0;
			if (task != null) status = task.status;
			Brush myBrush;
			if (status == 0)
				myBrush = Brushes.Yellow;
			else if (status == 1)
				myBrush = Brushes.Green;
			else
				myBrush = Brushes.Red;
			e.Graphics.DrawString(taskName, e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
			e.DrawFocusRectangle();
		}

		private void issueTaskToolStripMenuItem_Click(object sender, EventArgs e) {
			if (listBox1.SelectedIndex == ListBox.NoMatches) {
				MessageBox.Show("No task exists or selected!");
				return;
			}

			if (controller.ContainSubmission(GetSelectedTaskName())) {
				DialogResult dr = MessageBox.Show("There exists a copy of the task's submission now, do you want to erase it?", "Confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
				if (dr == DialogResult.OK) {
					controller.CleanSubmission(GetSelectedTaskName());
				} else {
					return;
				}
			}

			controller.GetTask(GetSelectedTaskName()).status = 1;
			listBox1.Refresh();
			MessageBox.Show("Task has been issued");
		}

		private void deleteTaskToolStripMenuItem_Click(object sender, EventArgs e) {
			if (listBox1.SelectedIndex == ListBox.NoMatches) {
				MessageBox.Show("No task exists or selected!");
				return;
			}
			string taskName = GetSelectedTaskName();
			controller.DeleteTask(taskName);
			listBox1.Items.RemoveAt(listBox1.SelectedIndex);
			TabPage tabPage = GetTabPageByTaskName(taskName);
			if (tabPage != null) tabControl1.TabPages.Remove(tabPage);
			tabPage = GetTabPageAnalysisByTaskName(taskName);
			if (tabPage != null) tabControl1.TabPages.Remove(tabPage);
			MessageBox.Show("Task has been deleted");
		}

		private void finishTaskToolStripMenuItem_Click(object sender, EventArgs e) {
			if (listBox1.SelectedIndex == ListBox.NoMatches) {
				MessageBox.Show("No task exists or selected!");
				return;
			}
			controller.GetTask(GetSelectedTaskName()).status = 2;
			listBox1.Refresh();
			MessageBox.Show("Task has finished");
		}

		private void showTaskToolStripMenuItem_Click(object sender, EventArgs e) {
			if (listBox1.SelectedIndex == ListBox.NoMatches) {
				MessageBox.Show("No task exists or selected!");
				return;
			}
			ShowTask(listBox1.SelectedIndex);
		}

		private void clearControlListToolStripMenuItem_Click(object sender, EventArgs e) {
			controller.ClearLimit();
			MessageBox.Show("Limit list has been cleared");
		}

		private void editControlListToolStripMenuItem_Click(object sender, EventArgs e) {
			EditLimitForm editLimitForm = new EditLimitForm(controller);
			editLimitForm.ShowDialog();
		}

		private void viewStudentStatusToolStripMenuItem_Click(object sender, EventArgs e) {
			StudentsStatusForm studentsStatusForm = new StudentsStatusForm(controller);
			studentsStatusForm.Show();
		}

		private void exportAnalysisToolStripMenuItem_Click(object sender, EventArgs e) {
			ExportResultForm exportResultForm = new ExportResultForm(controller);
			exportResultForm.ShowDialog();
		}

		private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
			AboutBox1 aboutBox1 = new AboutBox1();
			aboutBox1.ShowDialog();
		}

		private void deleteTopicToolStripMenuItem_Click(object sender, EventArgs e) {
			if (listBox2.SelectedIndex == ListBox.NoMatches) {
				MessageBox.Show("No topic exists or selected!");
				return;
			}
			string topicName = GetSelectedTopicName();
			controller.DeleteTopic(topicName);
			listBox2.Items.RemoveAt(listBox2.SelectedIndex);
			TabPage tabPage = GetTabPageByTopicName(topicName);
			if (tabPage != null) tabControl1.TabPages.Remove(tabPage);
			MessageBox.Show("Topic has been deleted");
		}

		private void issueTopicToolStripMenuItem_Click(object sender, EventArgs e) {
			if (listBox2.SelectedIndex == ListBox.NoMatches) {
				MessageBox.Show("No topic exists or selected!");
				return;
			}

			if (controller.ContainTeam(GetSelectedTopicName())) {
				DialogResult dr = MessageBox.Show("There exists a copy of the topic's team list now, do you want to erase it?", "Confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
				if (dr == DialogResult.OK) {
					controller.CleanTeam(GetSelectedTopicName());
				} else {
					return;
				}
			}

			controller.GetTopic(GetSelectedTopicName()).status = 1;
			listBox2.Refresh();
			MessageBox.Show("Topic has been issued");
		}

		private void listBox2_DrawItem(object sender, DrawItemEventArgs e) {
			e.DrawBackground();
			if (e.Index < 0) return;
			string topicName = listBox2.Items[e.Index].ToString();
			Topic topic = controller.GetTopic(topicName);
			int status = 0;
			if (topic != null) status = topic.status;
			Brush myBrush;
			if (status == 0)
				myBrush = Brushes.Yellow;
			else if (status == 1)
				myBrush = Brushes.Green;
			else
				myBrush = Brushes.Red;
			e.Graphics.DrawString(topicName, e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
			e.DrawFocusRectangle();
		}

		private void finishTopicToolStripMenuItem_Click(object sender, EventArgs e) {
			if (listBox2.SelectedIndex == ListBox.NoMatches) {
				MessageBox.Show("No topic exists or selected!");
				return;
			}
			Topic topic = controller.GetTopic(GetSelectedTopicName());
			topic.status = 2;
			foreach (Team t in topic.teams) {
				if (t.status == 0) topic.teams.Remove(t);
			}
			listBox2.Refresh();
			MessageBox.Show("Topic has finished");
		}

		private void showTopicToolStripMenuItem_Click(object sender, EventArgs e) {
			if (listBox2.SelectedIndex == ListBox.NoMatches) {
				MessageBox.Show("No topic exists or selected!");
				return;
			}
			ShowTopic(listBox2.SelectedIndex);
		}

		private void listBox2_MouseDoubleClick(object sender, MouseEventArgs e) {
			ShowTopic(listBox2.IndexFromPoint(e.Location));
		}

		private void exportAnalysisToolStripMenuItem1_Click(object sender, EventArgs e) {
			
		}

		private void resplitToolStripMenuItem_Click(object sender, EventArgs e) {
			ResplitForm resplitForm = new ResplitForm(controller);
			resplitForm.ShowDialog();
		}

		private void showTasksAnlysisToolStripMenuItem_Click(object sender, EventArgs e) {
			if (listBox1.SelectedIndex == ListBox.NoMatches) {
				MessageBox.Show("No task exists or selected!");
				return;
			}
			ShowTaskAnalysis(listBox1.SelectedIndex);
		}

		public void ShowTaskAnalysis(int index) {
			//MyConsole.WriteLine(index.ToString());
			if (index != ListBox.NoMatches) {  //valid click
				string taskName = listBox1.Text;
				TabPage tabPage = GetTabPageAnalysisByTaskName(taskName);
				if (tabPage == null) {
					tabPage = new TabPage("Analysis: " + taskName);
					tabPage.AutoScroll = true;
					tabPage.BackColor = Color.White;
					tabControl1.Controls.Add(tabPage);
					UpdateTabPage(controller.GetAllAnswer(taskName));
				}
				tabControl1.SelectedTab = tabPage;
			}
		}

		private void exportTopicTeamsToolStripMenuItem_Click(object sender, EventArgs e) {
			if (listBox2.Items.Count == 0) {
				MessageBox.Show("No topic exists!");
				return;
			}

			SaveFileDialog sfd = new SaveFileDialog();
			sfd.Filter = "Text File(*.txt)|*.txt";
			if (sfd.ShowDialog() == DialogResult.OK) {
				string localFilePath = sfd.FileName.ToString(); //acquire file path
				string path = localFilePath;
				if (localFilePath.Substring(localFilePath.Length - 4, 4) != ".txt")
					path += ".txt";
				controller.ExportTopicsAndTeams(path);
				System.Diagnostics.Process.Start("notepad.exe ", path); 
			}
		}

	}
}
